﻿namespace TransferObjects
{
    using System;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    using System.Linq;

    [Serializable]
    public class YahooResults
    {
        public List<YahooResult> Result { get; set; }

        public List<object> Table {
            set
            {
                if (value != null)
                {
                    this.Tables = new List<YahooTable>();

                    foreach (object t in value)
                    {
                        YahooTable tableObj = new YahooTable();

                        if ((t as string) != null)
                        {
                            tableObj.Content = t.ToString();
                        }
                        else
                        {
                            IDictionary<string, object> dic = (IDictionary<string, object>)t;
                            List<string> dicKeys = dic.Keys.Select(k => k.ToLower()).ToList();
                            if (dicKeys.Contains("src"))
                            {
                                tableObj.Src = dic["src"] != null ? dic["src"].ToString() : null;
                            }

                            if (dicKeys.Contains("security"))
                            {
                                tableObj.Security = dic["security"] != null ? dic["security"].ToString() : null;
                            }

                            if (dicKeys.Contains("content"))
                            {
                                tableObj.Content = dic["content"] != null ? dic["content"].ToString() : null;
                            }
                        }

                        this.Tables.Add(tableObj);
                    }
                }
            }
        }

        public YahooQuote Quote { get; set; }
        public List<YahooTable> Tables { get; set; }
    }
}
